
package it.intecs.pisa.smarcos.util;

import java.io.InputStream;
import java.lang.reflect.Field;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;

public class ContextUtils {
	private static final String TAG = ContextUtils.class.getSimpleName();
	
	private Context context;

	public ContextUtils(Context c){

		context = c;
	}
	
	/**
	 * THis method processes the string by replacing %d markers with parameters
	 * @param string
	 * @param values
	 * @return
	 */
	public String processString(String string, String... values)
	{
		String processedString = "";
		int index=0,i=0;
		String before,after;

		Log.d(TAG, string);

		//checking if string is a reference to resources
		if(string.startsWith("@string/") || string.startsWith("@+string/"))
		{
			int rValue = retrieveRvalue("string",string.substring(string.indexOf("/")+1));
			if (rValue != 0)
				processedString=context.getString(rValue);
			else
				Log.d(TAG, string + " not present in R.string");
		}
		else processedString=string;

		while((index=processedString.indexOf("%d"))>-1)
		{
			before=processedString.substring(0,index);
			after=processedString.substring(index+2);
			processedString=before+values[i]+after;
			i++;
		}

		return processedString;
	}

	public int retrieveRvalue(String fromWhere, String fieldName){

		int value = 0;
		String name = "R." + fromWhere + "." + fieldName;
		Field  layout;
		try {
			layout = Class.forName(context.getPackageName() + ".R$" + fromWhere).getField(fieldName);
			value = layout.getInt(name);
			Log.d(TAG, "" + value);

		} catch (ClassNotFoundException e) {
			Log.d(TAG, "" + e);

		} catch (LinkageError e) {
			Log.d(TAG, "" + e);

		} catch (NoSuchFieldException e) {
			Log.d(TAG, "" + e);

		} catch (IllegalAccessException e) {
			Log.d(TAG, "" + e);

		} 

		return value;		
	}
	
	public String retriveContactFromNumber(String number) {

		Log.d(TAG, "retriveContactFromNumber");

		String name = null;
		String contactId = null;
	
		// define the columns I want the query to return
		String[] projection = new String[] {
		        ContactsContract.PhoneLookup.DISPLAY_NAME,
		        ContactsContract.PhoneLookup._ID};

		// encode the phone number and build the filter URI
		Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

		// query time
		Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

		if (cursor.moveToFirst()) {

		    // Get values from contacts database:
		    contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
		    name =      cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
		
		    Log.d(TAG, "retriveContactFromNumber: Contact Found @ " + number);            
		    Log.d(TAG, "retriveContactFromNumber: Contact name  = " + name);
		    Log.d(TAG, "retriveContactFromNumber: Contact id    = " + contactId);

		} else
		    Log.d(TAG, "retriveContactFromNumber: Contact Not Found @ " + number);
		
		return name;
	}
}